package ch.ethz.fcl.udp;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

import android.util.Log;

public class UDPClient implements Runnable {
	private final String TAG = "UDP CLIENT";
	private static String message = "MetroBuzz:Info:Hello from Client";
	public static String ip = "192.168.0.20";
	private final static int SERVERPORT = 5037;

	public UDPClient() {

	}

	public UDPClient(String msg) {
		message = msg;
	}

	public void run() {
		try {
			// Retrieve the ServerName
			InetAddress serverAddr = InetAddress.getByName(ip);

			/* Create new UDP-Socket */
			DatagramSocket socket = new DatagramSocket();
			byte[] buf = message.getBytes();

			DatagramPacket packet = new DatagramPacket(buf, buf.length,
					serverAddr, SERVERPORT);
			Log.d(TAG, "C: Sending: '" + new String(buf) + "'");

			socket.send(packet);
		} catch (Exception e) {
			Log.d(TAG, "Client Error");
		}
	}

	public void setMessage(String msg) {
		message = msg;
	}

	public void setMessage(int msg) {
		message = msg + "";
	}
}
